package com.senlan.water.dao.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.senlan.water.common.constant.BooleanLogicEnum;
import com.senlan.water.common.exception.BusinessException;
import com.senlan.water.common.result.ResultCode;
import com.senlan.water.common.util.DateUtil;
import com.senlan.water.common.util.UserInfoUtil;
import com.senlan.water.dao.domain.ShortMessageTemplate;
import com.senlan.water.dao.domain.User;
import com.senlan.water.dao.service.ShortMessageTemplateService;
import com.senlan.water.dao.mapper.ShortMessageTemplateMapper;
import com.senlan.water.dto.sms.ShortMessageTemplateAddDTO;
import com.senlan.water.dto.sms.ShortMessageTemplateEditDTO;
import com.senlan.water.dto.sms.ShortMessageTemplateSearchDTO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author 10499
* @description 针对表【short_message_template(短信模板)】的数据库操作Service实现
* @createDate 2024-12-11 09:45:04
*/
@Service
public class ShortMessageTemplateServiceImpl extends ServiceImpl<ShortMessageTemplateMapper, ShortMessageTemplate>
    implements ShortMessageTemplateService{

    @Resource
    private UserInfoUtil userInfoUtil;

    @Override
    public Boolean addNew(ShortMessageTemplateAddDTO addDTO) {
        ShortMessageTemplate shortMessageTemplate = new ShortMessageTemplate();
        BeanUtil.copyProperties(addDTO,shortMessageTemplate);
        //把手机号的list转换为字符串逗号拼接
        if (CollectionUtil.isNotEmpty(addDTO.getPhoneList())){
            shortMessageTemplate.setPhones(String.join(",",addDTO.getPhoneList()));
        }

        //设置用户信息
        User userInfo = userInfoUtil.getUserInfo();
        shortMessageTemplate.setCreateBy(userInfo.getNickname());
        shortMessageTemplate.setUpdateBy(userInfo.getNickname());
        return baseMapper.insert(shortMessageTemplate) > 0;
    }

    @Override
    public Boolean editById(ShortMessageTemplateEditDTO editDTO) {
        ShortMessageTemplate shortMessageTemplate = baseMapper.selectById(editDTO.getId());
        if (shortMessageTemplate == null){
            throw new BusinessException(ResultCode.SMS_DATA_NOT_EXITS);
        }

        BeanUtil.copyProperties(editDTO,shortMessageTemplate);
        //把手机号的list转换为字符串逗号拼接
        if (CollectionUtil.isNotEmpty(editDTO.getPhoneList())){
            shortMessageTemplate.setPhones(String.join(",",editDTO.getPhoneList()));
        }

        //设置用户信息
        User userInfo = userInfoUtil.getUserInfo();
        shortMessageTemplate.setUpdateBy(userInfo.getNickname());
        return baseMapper.updateById(shortMessageTemplate) > 0;
    }

    @Override
    public Boolean enable(Long id) {
        ShortMessageTemplate shortMessageTemplate = baseMapper.selectById(id);
        if (shortMessageTemplate == null){
            throw new BusinessException(ResultCode.SMS_DATA_NOT_EXITS);
        }

        shortMessageTemplate.setStatus(BooleanLogicEnum.LOGIC_TRUE.getCode());
        return baseMapper.updateById(shortMessageTemplate) > 0;
    }

    @Override
    public Boolean disable(Long id) {
        ShortMessageTemplate shortMessageTemplate = baseMapper.selectById(id);
        if (shortMessageTemplate == null){
            throw new BusinessException(ResultCode.SMS_DATA_NOT_EXITS);
        }

        shortMessageTemplate.setStatus(BooleanLogicEnum.LOGIC_FALSE.getCode());
        return baseMapper.updateById(shortMessageTemplate) > 0;
    }

    @Override
    public Boolean deleteById(Long id) {
        ShortMessageTemplate shortMessageTemplate = baseMapper.selectById(id);
        if (shortMessageTemplate == null){
            throw new BusinessException(ResultCode.SMS_DATA_NOT_EXITS);
        }
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    public Page<ShortMessageTemplate> getPage(ShortMessageTemplateSearchDTO searchDTO) {
        LambdaQueryWrapper<ShortMessageTemplate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(searchDTO.getTitle()),ShortMessageTemplate::getTitle,searchDTO.getTitle());
        queryWrapper.like(StrUtil.isNotBlank(searchDTO.getMessageTemplate()),ShortMessageTemplate::getMessageTemplate,searchDTO.getMessageTemplate());
        queryWrapper.like(StrUtil.isNotBlank(searchDTO.getPhones()),ShortMessageTemplate::getPhones,searchDTO.getPhones());
        queryWrapper.eq(searchDTO.getStatus() != null,ShortMessageTemplate::getStatus,searchDTO.getStatus());
        if (CollectionUtil.isNotEmpty(searchDTO.getCreateTime())){
            queryWrapper.ge(ShortMessageTemplate::getCreateTime,searchDTO.getCreateTime().get(0));
            queryWrapper.le(ShortMessageTemplate::getCreateTime, DateUtil.convertDate(searchDTO.getCreateTime().get(1)));
        }

        Page<ShortMessageTemplate> page = new Page<>();
        page.setCurrent(searchDTO.getPageNum());
        page.setSize(searchDTO.getPageSize());
        baseMapper.selectPage(page,queryWrapper);
        return page;
    }
}




